package com.peakmain.wanandroid.projectmodule.fragment;

import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.andview.refreshview.XRefreshView;
import com.peakmain.baselibrary.recyclerview.listener.OnItemClickListener;
import com.peakmain.wanandroid.netmodule.bean.project.ProjectListBean;
import com.peakmain.wanandroid.netmodule.constant.Constant;
import com.peakmain.wanandroid.netmodule.utils.XRefreshUtils;
import com.peakmain.wanandroid.projectmodule.R;
import com.peakmain.wanandroid.projectmodule.R2;
import com.peakmain.wanandroid.projectmodule.adapter.ProjectListAdapter;
import com.peakmain.wanandroid.projectmodule.base.BaseProjectFragment;
import com.peakmain.wanandroid.projectmodule.contract.ProjectListContract;
import com.peakmain.wanandroid.projectmodule.presenter.ProjectListPresenter;

import butterknife.BindView;

/**
 * author: peakmain
 * createdata：2019/7/12
 * mail: 2726449200@qq.com
 * desiption:
 */
public class ProjectListFragment extends BaseProjectFragment<ProjectListPresenter> implements ProjectListContract.View {
    private int id;
    @BindView(R2.id.xrefresh_view)
    protected XRefreshView mXrefreshView;
    @BindView(R2.id.recycler_view)
    protected RecyclerView mRecyclerView;
    private int pageNum = 1;
    private int totalPage = 1;
 private ProjectListAdapter mProjectListAdapter;
    public static ProjectListFragment newInstance(int id) {
        ProjectListFragment itemFragment = new ProjectListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        itemFragment.setArguments(bundle);
        return itemFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragement_project_list;
    }

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected void initView(View view) {
        Bundle bundle = getArguments();
        if (bundle != null) {
            id = bundle.getInt("id");

        }
        initRecyclerView();
    }

    private void initRecyclerView() {
        XRefreshUtils.getInstance().initXRefresh(mXrefreshView,getContext());
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        mXrefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(boolean isPullDown) {
                getList(true, false);
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                getList(false, true);
            }
        });
    }

    private void getList(final boolean isPullDown, final boolean isSilence) {
        if (isPullDown) {
            pageNum = 1;
        }
        if (isSilence) {
            if (pageNum > totalPage) {
                return;
            }
        }
        mPresenter.getProjectList(pageNum, id, isPullDown, isSilence);
    }

    @Override
    public void onResume() {
        if (mXrefreshView != null) {
            mXrefreshView.startRefresh();
        }
        super.onResume();
    }


    @Override
    public void showProjectListSuccess(ProjectListBean bean, boolean isPullDown, boolean isSilence) {
        mXrefreshView.stopRefresh();
        mXrefreshView.stopLoadMore();
        setData(bean);
    }

    private void setData(final ProjectListBean result) {
        pageNum = result.getCurPage() + 1;
        totalPage = result.getTotal();
        mProjectListAdapter=new ProjectListAdapter(getContext(),result.getDatas());
        mRecyclerView.setAdapter(mProjectListAdapter);
        mProjectListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                startBrowserActivity(getContext(), Constant.MODE_DEFAULT,result.getDatas().get(position).getLink());
            }
        });
        if (pageNum > totalPage) {
            mXrefreshView.setLoadComplete(true);
        }

    }

}